var WastageModel = function (wastage) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (wastage) {
      var fields = ['deptId', 'deptName', 'workShopId', 'workShopName'];
      self.setDataFromObj(wastage, fields);

      self.id = `${self.deptId}-${self.workShopId}`;

      var dayList = wastage.dayList;
      var wastageRecordList = [];
      var wastageDataFields = ['electricUse', 'gasUse', 'waterUse'];
      var monthElectricUse = 0,
        monthGasUse = 0,
        monthWaterUse = 0,
        monthCapacity = 0,
        maxDataValue = 0;
      for (var i = 0; i < dayList.length; i++) {
        var dayData = dayList[i];
        var dayTime = dayData.dayTime;
        if (SysUtil.isEmpty(dayTime)) {
          continue;
        }

        var capacity = Number(dayData.capacity);
        var wastageData = {
          dayTime: dayTime,
          capacity: capacity
        };
        for (var j = 0; j < wastageDataFields.length; j++) {
          var field = wastageDataFields[j];
          var value = dayData[field];
          value = Number(value);
          value *= 1000;
          wastageData[field] = value; //电:w·h 气、水:升
        }

        var perElectricUse = 0,
          perGasUse = 0,
          perWaterUse = 0;
        if (capacity !== 0) {
          perElectricUse = _.round(wastageData.electricUse / capacity, 4);
          perGasUse = _.round(wastageData.gasUse / capacity, 4);
          perWaterUse = _.round(wastageData.waterUse / capacity, 4);
          var maxValue = _.max([perElectricUse, perGasUse, perWaterUse]);
          if (maxDataValue < maxValue) {
            maxDataValue = maxValue;
          }
        }
        wastageData.perElectricUse = perElectricUse;
        wastageData.perGasUse = perGasUse;
        wastageData.perWaterUse = perWaterUse;

        monthElectricUse += wastageData.electricUse;
        monthGasUse += wastageData.gasUse;
        monthWaterUse += wastageData.waterUse;
        monthCapacity += wastageData.capacity;
        wastageRecordList.push(wastageData);
      }
      self.maxDataValue = maxDataValue;
      self.monthElectricUse = monthElectricUse;
      self.monthGasUse = monthGasUse;
      self.monthWaterUse = monthWaterUse;
      self.monthCapacity = monthCapacity;

      var dateList = _.map(wastageRecordList, 'dayTime');
      var minDate = moment().startOf('month');
      while (moment().isSameOrAfter(minDate, 'day')) {
        var date = minDate.format('YYYY-MM-DD');
        var index = dateList.indexOf(date);
        if (index === -1) {
          wastageRecordList.push({
            dayTime: date,
            electricUse: 0,
            perElectricUse: 0,
            gasUse: 0,
            perGasUse: 0,
            waterUse: 0,
            perWaterUse: 0,
            capacity: 0
          });
        }
        minDate.add(1, 'day');
      }

      wastageRecordList = _.orderBy(wastageRecordList, [function (record) {
        return moment(record.dayTime).valueOf();
      }], ['asc']);

      var toDayWastageRecord = wastageRecordList[wastageRecordList.length - 1];
      self.dayElectricUse = toDayWastageRecord.electricUse;
      self.dayGasUse = toDayWastageRecord.gasUse;
      self.dayWaterUse = toDayWastageRecord.waterUse;
      self.dayCapacity = toDayWastageRecord.capacity;

      self.wastageRecordList = wastageRecordList;
    }
  };

  fn.initData(wastage);
};